import { ref } from 'vue';
import { defineStore } from 'pinia';
import type { IRouteTree } from '@/types/route.d';
import type { IMenuTree } from '@/types/menu.d';

interface IUserInfo {
  avtrUrl: string;
  nickNm: string;
  genderCd: string;
  persnSign: string;
  empId: string;
  empNo: string;
  empCpln: string;
  orgId: string;
  orgNm: string;
  roleIdList: string[];
  orgList: {
    orgAdr: string;
    orgId: string;
    orgNm: string;
    orgNo: string;
  }[];
}

interface IToken {
  accessToken: string;
  expireTm: number;
  refreshToken: string;
}

export const useUserInfoStore = defineStore('userInfo', () => {
  const userInfo = ref<IUserInfo | null>(null);
  const menus = ref<IMenuTree[] | null>(null);
  const routes = ref<IRouteTree[] | null>(null);

  const getToken = (): IToken | null => {
    return JSON.parse(window.localStorage.getItem('token') ?? 'null');
  };
  const setToken = (val: IToken | null) => {
    window.localStorage.setItem('token', JSON.stringify(val));
  };
  const delToken = (): void => {
    window.localStorage.removeItem('token');
  };

  return {
    getToken,
    setToken,
    delToken,
    userInfo,
    menus,
    routes
  };
});
